package com.test.rpc.common;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;

public class ReflectionUtils {

    public static <T> T newInstance(Class<T> clazz){
        try{
            return clazz.newInstance();
        }
        catch (Exception e){
            throw new IllegalStateException(e);
        }
    }
    public static Method[] getPublicMethods(Class clazz){
        Method[] methods = clazz.getDeclaredMethods();
        ArrayList<Method> pmethods = new ArrayList<>();
        for (Method m: methods) {
            if(Modifier.isPublic(m.getModifiers())){
                pmethods.add(m);
            }
        }
        return pmethods.toArray(new Method[0]);
    }
    public static Object invoke(Object obj,Method method,Object...args){
        try{
                return method.invoke(obj,args);
        }catch (Exception e){
            throw new IllegalStateException();
        }
    }

}
